/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test1;

import com.yunhoo.base.model.Department;
import com.yunhoo.base.model.Organization;
import com.yunhoo.base.model.Person;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.login.LoginInfo;
import com.yunhoo.sys.navigation.MenuBean;
import com.yunhoo.sys.util.common.CommonParam;
import com.yunhoo.sys.util.common.FacesUtils;
import com.yunhoo.sys.util.common.ToolKit;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

@ManagedBean(name = "oalogin")
@RequestScoped
public class LoginWithOA {

    private Organization org = new Organization();
    private Department dept = new Department();
    private Person user = new Person();
    private Date loginDate = new Date();
    private Integer iyear = null;
    private String psn = "";

    public LoginWithOA() {
        org.setCode(CommonParam.defaulOrgCode());
        //iyear = Calendar.getInstance().get(Calendar.YEAR);
        iyear = CommonParam.defaultYear();
        System.out.println("loginWithOA iyear : " + iyear);
        test();
    }

    public final void test() {
        ExternalContext c = FacesContext.getCurrentInstance().getExternalContext();

        psn = c.getRequestParameterMap().get("psn");
        System.out.println(" psn : " + psn);

        login();

        HttpServletResponse response = (HttpServletResponse) c.getResponse();
        try {
            response.sendRedirect("/cea/main.jsf");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public String login() {
        Person param = new Person();
        param.setOrgCode(org.getCode());
        param.setIyear(iyear);
        param.setId(Integer.parseInt(psn.trim()));
        List<Person> perli = IbatisTemplate.queryForList("person.queryForOA", param);
        
        if (perli == null || perli.isEmpty() || perli.size() != 1) {
            user = initGuest();
        } else user = perli.get(0);

        if (user != null) {
            dept.setCode(user.getDeptCode());
            dept.setName(user.getDeptName());
            saveLoginInfoInSession();
            initMenuBeanWelcomePage();
            
            return "true";
        } else {
            user = new Person();
            ToolKit.msgs_err(FacesContext.getCurrentInstance(), null, "密码不正确,请重新输入");
            return "login";
        }
    }

    private Person initGuest() {
        Person per = new Person();
        per.setId(0);
        per.setCpsn_number("00000");
        per.setCpsn_name("游客");
        per.setRole("guest");
        per.setDeptCode("00");
        per.setDeptName("");
        return per;
    }

    private void saveLoginInfoInSession() {
        ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
        Map sessionMap = extContext.getSessionMap();
        sessionMap.put("loginInfo", new LoginInfo(iyear, org, dept, user, loginDate));
    }

    private void initMenuBeanWelcomePage() {
        MenuBean menuBean = (MenuBean) FacesUtils.getManagedBean("sysmenu");
        menuBean.initSelectedPage();
    }

    public Person getUser() {
        return user;
    }

    public void setUser(Person user) {
        this.user = user;
    }

    public String getPsn() {
        return psn;
    }

    public void setPsn(String psn) {
        this.psn = psn;
    }
}
